package com.yonyou.pmclouds.basecom.ref;

import com.yonyou.pmclouds.basecom.ref.impl.ReferenceCheckerImpl;
import com.yonyou.pmclouds.basecom.ref.itf.ReferenceCheckOfBusiness;
import com.yonyou.pmclouds.basecom.ref.itf.ReferenceChecker;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 引用校验的spring配置类
 * <br>
 * 使用bean后处理器，来向checker注册容器中的处理器
 */
@Slf4j
@Configuration
public class DataRefConfiguration implements BeanPostProcessor {
    private ReferenceCheckerImpl checker = new ReferenceCheckerImpl();

    @Bean
    public ReferenceChecker referenceChecker() {
        return checker;
    }

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        // 扫描所有的实现ref接口的bean，管理起来
        if (bean instanceof ReferenceCheckOfBusiness) {
            checker.addChecker((ReferenceCheckOfBusiness) bean);
        }
        return bean;
    }
}
